#include <GameEngine/Core/Classes/rtti.h>

// #include <iostream>

#ifdef GAMEENGINE_NAMESPACES
namespace GameEngine {
#endif

GAMEENGINE_IMPLEMENT_SINGLETON(RttiManager)


//! Default constructor
RttiManager::RttiManager() : rtti_id_(0) {}

//! Destructor
RttiManager::~RttiManager() {}

//! Assign a rtti number
/*!
 *  Increment the current rtti by 1 and return
 *  the result
 */
int RttiManager::assign_rtti(const GEString& name) {
	++rtti_id_;
//	std::cout << "Assign rtti for class \"" << name << "\" = " << rtti_id_ << std::endl;
	classes_.push_back(name);
	return rtti_id_;
}

//! Find or assign a rtti number
/*!
 *  If rtti number is not found, one is assigned for the class with given name
 */
int RttiManager::find_or_assign_rtti(const GEString& name) {
	// Try to find the rtti number
	int res = find_rtti(name);
	if ( res >= 0 ) return res;

	// Assign the rtti number
	return assign_rtti(name);
}

//! Find the rtti number of the class from its name
/*!
 @return The rtti number of the class from its name (-1 if there is no RTTI support for this class)
 */
int RttiManager::find_rtti(const GEString& name) const {
	int res = 0;

	// Iterate over the class names
	for ( std::vector< GEString >::const_iterator it = classes_.begin(); it != classes_.end(); ++it ) {
		if ( *it == name ) return res;
		++res;
	}

	// No such class has rtti support
	return -1;
}

//! Get the number of classes that requested RTTI support
int RttiManager::rtti_classes_count() const {
	return rtti_id_;
}

#ifdef GAMEENGINE_NAMESPACES
} // namespace GameEngine
#endif
